<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/3/25 19:13
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Ticket extends ADM_Controller
{

    // 分页数
    const PAGE_SIZE = 20;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 首页
     */
    public function index()
    {
        $this->load->view(parent::TMP_DIR . '/promoter/ticket/index.html');
    }

    /**
     * 添加
     */
    public function add()
    {
        $this->load->view(parent::TMP_DIR . '/promoter/ticket/add.html');
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $this->load->view(parent::TMP_DIR . '/promoter/ticket/edit.html');
    }

    /**
     * get ticket lists
     */
    public function getLists()
    {
        $send['state'] = false;

        do {

            $page = $this->input->get('page');
            $page = intval($page) <= 0 ? 1 : intval($page);
            $offset = ($page - 1) * self::PAGE_SIZE;
            $this->load->model('admin/promoter/Ticket_Model', 'ticket');
            $res = $this->ticket->getLists($offset, self::PAGE_SIZE);

            $send['state'] = true;
            $send['data'] = [
                'total' => intval($res['total']),
                'data' => $res['data']
            ];

        } while(false);

        $this->output->send($send);
    }

    /**
     * 添加优惠券
     */
    public function addTicket()
    {

        $send['state'] = false;

        do {
            $params = $this->input->post();
            $this->load->model('admin/promoter/Ticket_Model', 'ticket');
            $res = $this->ticket->add($params);
            if ($res) {
                $send['state'] = true;
                $send['data'] = ['msg' => '添加成功'];
            } else {
                $send['error'] = '添加失败';
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 编辑
     */
    public function doEdit()
    {
        $send['state'] = false;
        do {
            $params = $this->input->post();
            if (empty($params['id'])) {
                $send['error'] = 'params error';
                break;
            }
            $this->load->model('admin/promoter/Ticket_Model', 'ticket');
            $res = $this->ticket->edit($params);
            if ($res) {
                $send['state'] = true;
                $send['data'] = ['msg' => '添加成功'];
            } else {
                $send['error'] = '添加失败';
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 修改状态
     */
    public function changeStatus()
    {
        $send['state'] = false;
        do {

            $id = $this->input->post('id');
            $status = $this->input->post('status');
            if (empty($id) || $status == null) {
                $send['error'] = 'Params error';
                break;
            }

            $this->load->model('admin/promoter/Ticket_Model', 'ticket');
            if ($this->ticket->changStatus($id, $status)) {
                $send['state'] = true;
            } else {
                $send['error'] = '状态修改失败';
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 优惠券详情
     */
    public function ticketDetail()
    {
        $send['state'] = false;
        do {

            $id = $this->input->get('id');
            if (empty($id)) {
                $send['error'] = 'params error';
                break;
            }

            $this->load->model('admin/promoter/Ticket_Model', 'ticket');
            $detail = $this->ticket->get($id);
            if ($detail) {
                $send['state'] = true;
                $send['data'] = $detail;
            } else {
                $send['error'] = '查询失败';
            }

        } while(false);
        $this->output->send($send);
    }
}